package cn.colins.producer.service;

import cn.colins.api.TestService;
import com.alibaba.fastjson.JSON;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.rpc.RpcContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import java.util.Map;

/**
 * @program: dubbo-annotation-study
 * @description:
 * @author: colins
 * @create: 2023-05-06 14:00
 **/
@Component
@DubboService(protocol = {"dubbo","rest"},loadbalance = "roundrobin",weight = 10,parameters = {"cus","provider-2"},tag = "test11")
@Path("rest")
@Produces({"application/json; charset=UTF-8", "text/xml; charset=UTF-8"})
public class ProducerTestService implements TestService {
    private final static Logger log= LoggerFactory.getLogger(ProducerTestService.class);


    @GET()
    @Path("test")
    @Override
    public String test(@QueryParam("message") String message) {
        // 接收调用者传递过来的参数
        Map<String, Object> clientAttachments = RpcContext.getContext().getObjectAttachments();
        log.info("ContextService clientAttachments:" + JSON.toJSONString(clientAttachments));

        // 给调用者传递参数
        RpcContext.getServerContext().setAttachment("serverKey","client 你好！");

        // 返回
        return message;
    }
}
